﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StudentManagerPro.Student
{
    public partial class UpLoadImage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //获取新增学员Id
            if (!IsPostBack)
            {
                var studentId = Request.QueryString["id"];

                if (studentId == "")
                {
                    Response.Redirect("~/ErrorPage.html");
                }
                ViewState["StudentId"] = studentId;
            }
            this.ltaMsg.Text = "";

        }

        //上传照片
        protected void btnUpLoadImage_Click(object sender, EventArgs e)
        {
            //1.判断是否有文件
            if (!this.fulStuImage.HasFile) return;
            //2.获取文件大小
            double fileLength = Convert.ToDouble(this.fulStuImage.FileContent.Length / (1024.0 * 1024.0));
            if (fileLength > 1.0)
            {
                this.ltaMsg.Text = "<script type='text/javascript'>alert('图片最大不能超过1MB!')</script>";
                return;
            }
            //3.文件格式
            string fileName = this.fulStuImage.FileName;
            string fileExteion = fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower();
            if (fileExteion != "jpg" && fileExteion != "png")
            {
                this.ltaMsg.Text = "<script type='text/javascript'>alert('上传图片仅支持jpg与png格式!!')</script>";
                return;
            }
            //4.上传图片
            try
            {
                var path = Server.MapPath("~/Images/NewImages");
                fileName = path+"/"+ViewState["StudentId"].ToString()+ "jpg";
                this.fulStuImage.SaveAs(fileName);
                if (Request.QueryString["IsModify"].ToString() == "1")
                {
                    this.ltaMsg.Text = "<script type='text/javascript'>alert('图片修改成功!');location='StudentManage.aspx'</script>";

                }
                else
                {
                    this.ltaMsg.Text = "<script type='text/javascript'>alert('上传成功!');location='AddStudent.aspx'</script>";
                }
            }
            catch (Exception ex)
            {
                this.ltaMsg.Text = "<script type='text/javascript'>alert('上传失败：" + ex.Message + "')</script>";
            }
          


        }
    }
}